package org.tinygroup.uiml.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.tinygroup.commons.tools.CollectionUtil;
import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.logger.LogLevel;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;
import org.tinygroup.uiml.UiGenerator;
import org.tinygroup.uiml.UimlEngine;
import org.tinygroup.uiml.config.Component;
import org.tinygroup.uiml.config.Layout;
import org.tinygroup.uiml.config.layout.BorderLayout;
import org.tinygroup.uiml.config.layout.CardLayout;
import org.tinygroup.uiml.config.layout.ColumnsLayout;
import org.tinygroup.uiml.config.layout.GridLayout;
import org.tinygroup.uiml.config.layout.TabLayout;
import org.tinygroup.uiml.config.layout.XYLayout;
import org.tinygroup.uiml.config.layout.item.ColumnItem;
import org.tinygroup.uiml.config.layout.item.TabItem;
import org.tinygroup.uiml.config.layout.item.XYItem;

/**
 * 控制台模拟布局文件的代码生成机制
 * @author yancheng11334
 *
 */
public class ConsoleGenerator implements UiGenerator{

	private static final Logger logger = LoggerFactory.getLogger(ConsoleGenerator.class);
	
	public String getPlatform() {
		return "console";
	}

	public void generate(UimlEngine uimlManager) {
		Collection<Component> collection=uimlManager.getComponentList();
		if(CollectionUtil.isEmpty(collection)){
		   logger.logMessage(LogLevel.WARN, "组件列表不能为空");
		   return;
		}
		Iterator<Component> it = collection.iterator();
		while(it.hasNext()){
			generate(it.next());
		}
	}

	public void generate(UimlEngine uimlManager, String componentId) {
		if(StringUtil.isBlank(componentId)){
		   logger.logMessage(LogLevel.WARN, "组件Id不能为空");
		   return;
		}
		Component component = uimlManager.getComponent(componentId);
		if(component==null){
		   logger.logMessage(LogLevel.WARN, "没有找到Id[{}]的组件",componentId);
		   return;
		}
		generate(component);
	} 
	
	protected void generate(Component component) {
		//分情况调用生成接口
		if(component instanceof Layout){
			generateLayout((Layout)component);
		}else{
			generateComponent(component);
		}
	}
	
	/**
	 * 生成布局
	 * @param layout
	 */
	private void generateLayout(Layout layout){
		if(layout instanceof BorderLayout){
			generateBorderLayout((BorderLayout)layout);
		}else if(layout instanceof ColumnsLayout){
			generateColumnsLayout((ColumnsLayout)layout);
		}else if(layout instanceof CardLayout){
			generateCardLayout((CardLayout)layout);
		}else if(layout instanceof GridLayout){
			generateGridLayout((GridLayout)layout);
		}else if(layout instanceof TabLayout){
			generateTabLayout((TabLayout)layout);
		}else if(layout instanceof XYLayout){
			generateXYLayout((XYLayout)layout);
		}else{
			logger.logMessage(LogLevel.WARN, "Id[{}]的布局没有找到匹配的布局生成接口",layout.getId());
		}
	}
	
	/**
	 * 生成BorderLayout
	 * @param layout
	 */
    private void generateBorderLayout(BorderLayout layout){
    	generateHeadInfo(layout);
    	generate(layout.getTop());
    	generate(layout.getLeft());
    	generate(layout.getCenter());
    	generate(layout.getRight());
    	generate(layout.getBottom());
		generateTailInfo(layout);
	}
    
    /**
	 * 生成CardLayout
	 * @param layout
	 */
    private void generateCardLayout(CardLayout layout){
    	generateHeadInfo(layout);
    	List<Component> items = layout.getItems();
        if(items!=null){
        	for(Component component:items){
        		generate(component);
        	}
    	}
    	
		generateTailInfo(layout);
	}
    
    /**
	 * 生成GridLayout
	 * @param layout
	 */
    private void generateGridLayout(GridLayout layout){
    	generateHeadInfo(layout);
    	for(int i=0;i<layout.getRows();i++){
    		for(int j=0;j<layout.getColumns();j++){
    			generate(layout.getItem(i, j));
    		}
    	}
		generateTailInfo(layout);
	}
    
    /**
	 * 生成TabLayout
	 * @param layout
	 */
    private void generateTabLayout(TabLayout layout){
    	generateHeadInfo(layout);
    	List<TabItem> items = layout.getItems();
        if(items!=null){
        	for(TabItem item:items){
        		generate(item.getComponent());
        	}
    	}
    	
		generateTailInfo(layout);
	}
    
    /**
	 * 生成ColumnsLayout
	 * @param layout
	 */
    private void generateColumnsLayout(ColumnsLayout layout){
    	generateHeadInfo(layout);
    	List<ColumnItem> items = layout.getItems();
    	if(items!=null){
    		for(ColumnItem item:items){
        		generate(item.getComponent());
        	}
    	}
    	
		generateTailInfo(layout);
	}
    
    /**
	 * 生成XYLayout
	 * @param layout
	 */
    private void generateXYLayout(XYLayout layout){
    	generateHeadInfo(layout);
    	List<XYItem> items = layout.getItems();
        if(items!=null){
        	for(XYItem item:items){
        		generate(item.getComponent());
        	} 
    	}
    	
		generateTailInfo(layout);
	}
	
	/**
	 * 生成组件
	 * @param component
	 */
	private void generateComponent(Component component){
		generateHeadInfo(component);
		generateTailInfo(component);
	}
	
	private void generateHeadInfo(Component component){
		if(component==null){
		   return;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<").append(component.getType()).append(" id='").append(component.getId()).append("'");
		sb.append(" title='").append(component.getTitle()).append("'");
		sb.append(" >");
		System.out.println(sb.toString());
	}
	
    private void generateTailInfo(Component component){
    	if(component==null){
 		   return;
 		}
    	StringBuffer sb = new StringBuffer();
		sb.append("</").append(component.getType()).append(">");
		System.out.println(sb.toString());
	}

}
